package com.hex.ds.hdtp.core.web.data.table.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.api.R;
import com.hex.bigdata.hbda.annotation.Log;
import com.hex.ds.hdtp.core.app.common.page.dto.PageDto;
import com.hex.ds.hdtp.core.app.data.table.dto.excel.TableTransferListExcelDto;
import com.hex.ds.hdtp.core.app.data.table.dto.request.TableTransferListRequestDto;
import com.hex.ds.hdtp.core.app.data.table.service.ITableTransferListService;
import com.hex.ds.hdtp.core.web.common.page.PageVo;
import com.hex.ds.hdtp.core.web.common.page.converter.IPageVoAndPageDtoConverter;
import com.hex.ds.hdtp.core.web.data.table.converter.ITableTransferListVoConverter;
import com.hex.ds.hdtp.core.web.data.table.vo.TableTransferListVo;
import com.hex.ds.hdtp.core.web.data.table.vo.query.TableTransferListQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * @Package com.hex.ds.hdtp.core.web.data.table.controller
 * @ClassName TableTransferListController
 * @Description 表迁移清单-WEB接口
 * @Author Wang zhihao
 * @Date 2023/8/8 18:52
 * @Version v1.0
 **/
@Slf4j
@RestController
@Api(tags = "01-表迁移：清单")
@RequestMapping("/api/data/table/list")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class TableTransferListController {

    private final ITableTransferListService tableTransferListService;

    private final ITableTransferListVoConverter converter;

    private final IPageVoAndPageDtoConverter pageVoAndPageDtoConverter;

    /**
     * @param tableTransferListqueryVo
     * @param pageVo
     * @Method queryListByPage
     * @Param
     * @Return org.springframework.http.ResponseEntity
     * @Description 表迁移清单-分页查询
     * @Author Wang zhihao
     * @Date 2023/8/9 11:42
     * @Version V1.0
     */
    @GetMapping
    @ApiOperation("表迁移清单-分页查询")
    public ResponseEntity queryListByPage(TableTransferListQueryVo tableTransferListqueryVo, PageVo pageVo) {
        log.debug("进入到【TableTransferListController.queryListByPage】方法,参数：{}" + tableTransferListqueryVo.toString());
        final PageDto pageDto = pageVoAndPageDtoConverter.toPageDto(pageVo);
        TableTransferListRequestDto requestDto = converter.toRequestDto(tableTransferListqueryVo);
        return ResponseEntity.ok(tableTransferListService.queryListByPage(requestDto, pageDto));
    }

    /**
     * @Method queryPageListByTaskId
     * @Param tableTransferListqueryVo
     * @Param pageVo
     * @Return org.springframework.http.ResponseEntity
     * @Description 表迁移清单-根据任务主键分页查询
     * @Author gj.xu
     * @Date 2024/3/8 18:29
     * @Version V2.0
     */
    @GetMapping("/queryPageListByTaskId")
    @ApiOperation("表迁移清单-根据任务主键分页查询")
    public ResponseEntity queryPageListByTaskId(TableTransferListQueryVo tableTransferListqueryVo, PageVo pageVo) {
        log.debug("进入到【TableTransferListController.queryPageListByTaskId】方法,参数：{}" + tableTransferListqueryVo.toString());
        final PageDto pageDto = pageVoAndPageDtoConverter.toPageDto(pageVo);
        TableTransferListRequestDto requestDto = converter.toRequestDto(tableTransferListqueryVo);
        return ResponseEntity.ok(tableTransferListService.queryPageListByTaskId(requestDto, pageDto));
    }

    /**
     * @param tableTransferListqueryVo
     * @param pageVo
     * @Method queryLatestSuccess
     * @Param
     * @Return org.springframework.http.ResponseEntity
     * @Description 表迁移清单-分页查询每个建表最新成功的
     * @Author Wang zhihao
     * @Date 2023/8/28 11:02
     * @Version V1.0
     */
    @GetMapping("/latestSuccess")
    @ApiOperation("表迁移清单-分页查询每个建表最新成功的")
    public ResponseEntity queryLatestSuccess(TableTransferListQueryVo tableTransferListqueryVo, PageVo pageVo) {
        log.debug("进入到【TableTransferListController.queryLatestSuccess】方法,参数：{}" + tableTransferListqueryVo.toString());
        final PageDto pageDto = pageVoAndPageDtoConverter.toPageDto(pageVo);
        TableTransferListRequestDto requestDto = converter.toRequestDto(tableTransferListqueryVo);
        return ResponseEntity.ok(tableTransferListService.queryLatestSuccess(requestDto, pageDto));
    }

    /**
     * @param tableTransferListVo
     * @Method add
     * @Param
     * @Return org.springframework.http.ResponseEntity
     * @Description 表迁移清单-新增
     * @Author Wang zhihao
     * @Date 2023/8/9 11:43
     * @Version V1.0
     */
    @Log("表迁移清单-新增")
    @PostMapping
    @ApiOperation("表迁移清单-新增")
    public ResponseEntity add(@RequestBody TableTransferListVo tableTransferListVo) {
        log.info("进入到【TableTransferListController.add】方法,参数：{}", tableTransferListVo.toString());
        String pkId = tableTransferListService.addWithBackId(converter.toRequestDto(tableTransferListVo));
        return ResponseEntity.ok(pkId);
    }

    /**
     * @param tableTransferListVo
     * @Method update
     * @Param
     * @Return org.springframework.http.ResponseEntity
     * @Description 表迁移清单-更新
     * @Author Wang zhihao
     * @Date 2023/8/9 11:44
     * @Version V1.0
     */
    @PutMapping
    @ApiOperation("表迁移清单-更新")
    public ResponseEntity update(@RequestBody TableTransferListVo tableTransferListVo) {
        log.info("进入到【TableTransferListController.update】方法,主键：{}", tableTransferListVo.getPkId());
        tableTransferListService.modifyById(converter.toRequestDto(tableTransferListVo));
        return ResponseEntity.ok("更新成功");
    }

    /**
     * @param pkIds
     * @Method delete
     * @Param
     * @Return org.springframework.http.ResponseEntity
     * @Description 表迁移清单—批量删除
     * @Author Wang zhihao
     * @Date 2023/8/9 11:44
     * @Version V1.0
     */
    @Log("表迁移清单-批量删除")
    @DeleteMapping(value = {"/{pkIds}"})
    @ApiOperation("表迁移清单-批量删除")
    public ResponseEntity delete(@PathVariable("pkIds") @NotNull List<String> pkIds) {
        log.info("进入【TableTransferListController.delete】方法,参数：" + pkIds);
        tableTransferListService.batchRemoveById(pkIds);
        return ResponseEntity.ok("删除成功");
    }

    /**
     * @param pkId
     * @Method buildCreateTableSql
     * @Param
     * @Return com.hex.ds.hdtp.core.app.data.table.dto.response.TableTransferListResponseDto
     * @Description 表迁移清单-建立建表语句
     * @Author Wang zhihao
     * @Date 2023/8/10 15:00
     * @Version V1.1
     */
    @Log("表迁移清单-建立建表语句")
    @PutMapping(value = {"/buildSql/{pkId}"})
    @ApiOperation("表迁移清单-建立建表语句")
    public ResponseEntity buildCreateTableSql(@PathVariable("pkId") @NotNull String pkId) {
        log.info("进入【TableTransferListController.buildCreateTableSql】方法, 主键: {} ", pkId);
        return ResponseEntity.ok(tableTransferListService.buildCreateTableSql(pkId));
    }

    /**
     * @param sourceConnId
     * @param targetConnId
     * @param pkId
     * @Method getFieldMap
     * @Param
     * @Return org.springframework.http.ResponseEntity
     * @Description 表迁移清单-获取字段映射
     * @Author Wang zhihao
     * @Date 2023/8/14 14:44
     * @Version V1.0
     */
    @Log("表迁移清单-获取字段映射")
    @PutMapping(value = {"/getFieldMap/{sourceConnId}/{targetConnId}/{pkId}"})
    @ApiOperation("表迁移清单-获取字段映射")
    public ResponseEntity getFieldMap(@PathVariable("sourceConnId") @NotNull String sourceConnId,
                                      @PathVariable("targetConnId") @NotNull String targetConnId,
                                      @PathVariable("pkId") @NotNull String pkId) {
        log.info("进入【TableTransferListController.getFieldMap】方法,源连接编号:{}, 目标连接编号: {}, 主键: {}", sourceConnId, targetConnId, pkId);
        tableTransferListService.getFieldMap(sourceConnId, targetConnId, pkId);
        return ResponseEntity.ok("获取字段映射成功");
    }

    @Log("表迁移清单-导出迁移清单")
    @GetMapping(value = {"/exportTableTransfer"})
    @ApiOperation("表迁移清单-导出迁移清单")
    public ResponseEntity exportTableTransferList(@RequestParam List<String> pkIds) {
        log.info("进入【TableTransferListController.getFieldMap】方法,pkIds: {}", pkIds);
        ByteArrayResource bar = tableTransferListService.exportTableTransferList(pkIds);
        return ResponseEntity.ok()
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .header("Content-disposition", "attachment; filename=SourceInfoTemplate")
                .body(bar);
    }

    @Log("表迁移清单-导入迁移清单")
    @PostMapping(value = {"/importTableTransfer"})
    @ApiOperation("表迁移清单-导入迁移清单")
    public ResponseEntity importTableTransfer(@RequestBody MultipartFile file,@RequestParam String tableTransferListExcelDto) {
        log.info("进入【TableTransferListController.importTableTransfer】方法,MultipartFile: {},String:{}", file,tableTransferListExcelDto);
        Map<String,Object> resultMap = tableTransferListService.importTableTransferList(file,JSON.parseObject(tableTransferListExcelDto, TableTransferListExcelDto.class));
        return new ResponseEntity(resultMap,HttpStatus.OK);
    }
}
